<template>
    <div class="container">
        <div class="app-container">
            <!-- 角色管理内容 -->
            <div class="role-operate">
                <el-button size="mini" type="primary" @click="showDialog = true">添加角色</el-button>
            </div>
            <!-- 放置table组件 -->
            <el-table :data="list">
                <!-- 放置列 -->

                <el-table-column prop="name" align="center" width="200" label="角色">
                    <template v-slot="{ row }">
                        <!-- 条件判断 -->
                        <el-input v-if="row.isEdit" v-model="row.editRow.name" />
                        <span v-else>{{ row.name }}</span>
                    </template>
                </el-table-column>


                <el-table-column prop="state" align="center" width="200" label="启用">
                    <!-- 自定义结构 -->
                    <template v-slot="{ row }">

                        <el-switch v-if="row.isEdit" v-model="row.editRow.state" :active-value="1"
                            :inactive-value="0" />
                        <span v-else>{{ row.state === 1 ? '已启用' : row.state === 0 ? '未启用' : '无' }}</span>

                    </template>
                </el-table-column>

                <el-table-column prop="description" align="center" label="描述">

                    <template v-slot="{ row }">
                        <el-input v-if="row.isEdit" type="textarea" v-model="row.editRow.description" />
                        <span v-else>{{ row.description }}</span>
                    </template>

                </el-table-column>

                <el-table-column align="center" label="操作">

                    <template v-slot="{ row }">
                        <template v-if="row.isEdit">
                            <!-- 编辑状态 -->
                            <el-button type="primary" size="mini" @click="btnEditOK(row)">确定</el-button>
                            <el-button size="mini" @click="row.isEdit = false">取消</el-button>
                        </template>

                        <template v-else>
                            <!-- 放置操作按钮 -->
                            <el-button size="mini" type="text">分配权限</el-button>
                            <el-button size="mini" type="text" @click="btnEditRow(row)">编辑</el-button>

                            <el-popconfirm title="确定要删除吗？" @onConfirm="confirmDel(row.id)">
                                <el-button slot="reference" style="margin-left: 10px;" size="mini" type="text">删除</el-button>
                            </el-popconfirm>

                        </template>
                    </template>

                </el-table-column>

            </el-table>
            <!-- 放置分页组件 -->
            <el-row type="flex" justify="end" align="middle" style="height: 60px;">
                <!-- 分页组件 -->
                <el-pagination :page-size="pageParams.pagesize" :current-page="pageParams.page"
                    :total="pageParams.total" layout="total,prev,pager,next" @current-change="changePage" />
                <!--  < 1 2 3 4 >   -->
            </el-row>
        </div>
        <!-- dialog  弹窗 -->
        <el-dialog width="500px" title="新增角色" :visible.sync="showDialog" @close="btnCancel">
            <!-- 表单 -->
            <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width=120px>
                <el-form-item prop="name" label="角色名称">
                    <el-input v-model="roleForm.name" style="width: 300px;" size="mini" />
                </el-form-item>
                <el-form-item prop="name" label="启动">
                    <el-switch v-model="roleForm.state" :active-value="1" :inactive-value="0" size="mini" />
                </el-form-item>
                <el-form-item prop="description" label="角色描述">
                    <el-input v-model="roleForm.description" type="textarea" :row="8" style="width: 300px;"
                        size="mini" />
                </el-form-item>

                <el-form-item>
                    <el-row type="flex" justify="center">
                        <el-col :span="12">
                            <el-button type="primary" size="mini" @click="btnOK">确定</el-button>
                            <el-button size="mini" @click="btnCancel">取消</el-button>
                        </el-col>
                    </el-row>
                </el-form-item>
            </el-form>
        </el-dialog>
    </div>
</template>

<script>
import { getRoleList, addRole, updateRole,delRole } from "@/api/role";
export default {
    name: 'Role',
    data() {
        return {
            list: [],
            pageParams: {
                page: 1,// 第几页
                pagesize: 5, // 每页多少条
                total: 0 // 一共多少条
            },
            showDialog: false,
            roleForm: {
                name: '',
                description: '',
                state: 0
            },
            rules: {
                name: [{
                    required: true,//必填项
                    message: '角色名也没写啊！',
                    trigger: 'blur',//触发时机
                }],
                description: [{
                    required: true,//必填项
                    message: '描述也没写啊！',
                    trigger: 'blur',//触发时机
                }]
            }
        }
    },
    methods: {
        // 确定删除
        async confirmDel(){
            await delRole()
            this.$message.success('删除角色成功~')

            // 删除的是最后一条数据
            if(this.list.length === 1) this.pageParams.page--
            this.getRoleList()

        },
        async btnEditOK(row) {
            // 编辑状态下 点击确定触发
            if (row.editRow.name && row.editRow.description) {
                // 下一步
                await updateRole({ ...row.editRow, id: row.id })
                // 更新成功
                this.$message.success('更新角色成功~')
                // 退出 编辑状态
                // row.name = row.editRow.name   ESLint的校验 误判
                Object.assign(row, {
                    ...row.editRow,
                    isEdit: false //  退出编辑模式
                })// 规避eslint的误判
            } else {
                this.$message.warning('角色和描述不能为空~')
            }

        },
        btnEditRow(row) {
            row.isEdit = true

            // 更新缓存数据,重新显示原始值
            row.editRow.name = row.name
            row.editRow.state = row.state
            row.editRow.description = row.description
        },
        btnOK() {
            this.$refs.roleForm.validate(async isOK => {
                if (isOK) {
                    await addRole(this.roleForm)
                    this.$message.success('新增角色成功~')
                    this.getRoleList() // 刷新数据
                    this.btnCancel()
                }
            })
        },
        btnCancel() {
            this.roleForm = {
                name: '',
                description: '',
                state: 0
            }
            this.$refs.roleForm.resetFields()
            this.showDialog = false
        },
        async getRoleList() {
            // 接口
            const { rows, total } = await getRoleList()  // {rows} = {....}
            this.list = rows
            this.pageParams.total = total

            // 编辑
            this.list.forEach(item => {
                // item.isEdit = false
                this.$set(item, 'isEdit', false)
                this.$set(item, 'editRow', {
                    name: item.name,
                    state: item.state,
                    description: item.description
                })
            })

        },
        changePage(newPage) {
            this.pageParams.page = newPage  // 赋值当前新页码
            this.getRoleList() // 重新获取数据
        }
    },
    created() {  // 钩子
        this.getRoleList()
    }
}
</script>

<style>
.role-operate {
    padding: 10px;
}
</style>